﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Noebe;
using Pixysoft.Tools;
using System.Data;

namespace Pixysoft.Weblications.Sango
{
    class EntityNoebeTransaction : IEntityNoebeTransaction
    {
        INoebeTransaction transaction = null;

        public EntityNoebeTransaction(INoebeTransaction transaction)
        {
            this.transaction = transaction;
        }

        #region IEntityNoebeTransaction 成员

        public bool IsOpen
        {
            get
            {
                return transaction.IsOpen;
            }
        }

        public void Begin()
        {
            transaction.Begin();
        }

        public void Begin(System.Data.IsolationLevel level)
        {
            transaction.Begin(level);
        }

        public void Begin(Pixysoft.Framework.Noebe.NoebeTransactionArea transactionArea)
        {
            transaction.Begin(transactionArea);
        }

        public void Begin(System.Data.IsolationLevel level, Pixysoft.Framework.Noebe.NoebeTransactionArea transactionArea)
        {
            transaction.Begin(level, transactionArea);
        }

        public void Commit()
        {
            transaction.Commit();
        }

        public void Rollback()
        {
            transaction.Rollback();
        }

        #endregion

        #region IEntitySession 成员

        public int AutoInsert(params Pixysoft.Framework.Noebe.IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.CreateDate = GlobalTimer.Instance.GetGlobalTime();
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = NoebeManager.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return transaction.AutoInsert(table);
        }

        public void CascadeDelete(params Pixysoft.Framework.Noebe.IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.CreateDate = GlobalTimer.Instance.GetGlobalTime();
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = NoebeManager.EntityToDataTable(entities);

            if (table == null)
                return;

            transaction.CascadeDelete(table);
        }

        public int Delete(params Pixysoft.Framework.Noebe.IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.CreateDate = GlobalTimer.Instance.GetGlobalTime();
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = NoebeManager.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return transaction.Delete(table);
        }

        public int Insert(params Pixysoft.Framework.Noebe.IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.CreateDate = GlobalTimer.Instance.GetGlobalTime();
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = NoebeManager.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return transaction.Insert(table);
        }

        public int Update(params Pixysoft.Framework.Noebe.IEntityBase[] entities)
        {
            foreach (IEntityBase entity in entities)
            {
                entity.CreateDate = GlobalTimer.Instance.GetGlobalTime();
                entity.ModiDate = GlobalTimer.Instance.GetGlobalTime();
            }

            DataTable table = NoebeManager.EntityToDataTable(entities);

            if (table == null)
                return 0;

            return transaction.Update(table);
        }

        #endregion
    }
}
